package cn.edu.neusoft.cqy917.zyfypt_cqy917.model;

import java.util.List;

import cn.edu.neusoft.cqy917.zyfypt_cqy917.bean.VideoBean;
import cn.edu.neusoft.cqy917.zyfypt_cqy917.iface.VideoListener;
import cn.edu.neusoft.cqy917.zyfypt_cqy917.iface.Videoiface;
import cn.edu.neusoft.cqy917.zyfypt_cqy917.service.VideoService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;



public class VideoModel implements Videoiface {
    private VideoService videoService;
    private Retrofit retrofit;
    private static final String BASE_URL = "http://amicool.neusoft.edu.cn/";
    public VideoModel(){
        retrofit = new Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create()).build();
    }

    @Override
    public void getResultList(String mod, int page, String sessionID, final VideoListener videoListener) {
        videoService = retrofit.create(VideoService.class);
        Call<List<VideoBean>> call = videoService.getResult(mod, page, sessionID);
        call.enqueue(new Callback<List<VideoBean>>() {
            @Override
            public void onResponse(Call<List<VideoBean>> call, Response<List<VideoBean>> response) {
                if (response.isSuccessful()&&response.body()!=null)
                    videoListener.onResponse(response.body());
                else
                    videoListener.onFail("error");
            }

            @Override
            public void onFailure(Call<List<VideoBean>> call, Throwable t) {
                videoListener.onFail("error");
            }
        });
    }
}

